//
//  UserInfoView.m
//  jzyStudents
//
//  Created by 任辰东 on 2022/9/21.
//

#import "UserInfoView.h"

@interface UserInfoView ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *schoolLabel;
@property (nonatomic, strong) UILabel *classLabel;
@end

@implementation UserInfoView

-(void)loginBtnAction
{
    if (self.LoginBtnClicked) {
        self.LoginBtnClicked();
    }
}

-(void)setupViews
{
    [self addSubview:self.avatarView];
    [self addSubview:self.loginBtn];
    [self addSubview:self.nameLabel];
    [self addSubview:self.schoolLabel];
    [self addSubview:self.classLabel];
    
    [self.loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [RACObserve([GVUserDefaults standardUserDefaults], isLoginSuccess) subscribeNext:^(NSNumber *  _Nullable x) {
        
        if (x.boolValue) {
            self.loginBtn.hidden = YES;
            self.nameLabel.hidden = NO;
            self.schoolLabel.hidden = NO;
            self.classLabel.hidden = NO;
            self.nameLabel.text = [NSString stringWithFormat:@"姓名:%@",[GVUserDefaults standardUserDefaults].student_name];
            self.schoolLabel.text = [NSString stringWithFormat:@"学校:%@",[GVUserDefaults standardUserDefaults].organ_name];
            self.classLabel.text = [NSString stringWithFormat:@"班级:%@",[GVUserDefaults standardUserDefaults].class_name];
            
        }else{
            self.loginBtn.hidden = NO;
            self.nameLabel.hidden = YES;
            self.schoolLabel.hidden = YES;
            self.classLabel.hidden = YES;
        }
        
    }];
    
}

-(void)setupLayout
{
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self).offset(20);
        make.centerX.equalTo(self);
        make.width.height.mas_equalTo(45);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self);
        make.top.equalTo(self.avatarView.mas_bottom).offset(20);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(55);
        make.bottom.equalTo(self).offset(-15);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self.loginBtn);
    }];
    
    [self.schoolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.loginBtn);
        make.left.equalTo(self.nameLabel.mas_right);
        make.centerX.equalTo(self);
    }];
    
    [self.classLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.schoolLabel.mas_right);
        make.right.equalTo(self).offset(-15);
        make.centerY.equalTo(self.loginBtn);
    }];
    
    [self.schoolLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}

-(UIImageView *)avatarView
{
    if (!_avatarView) {
        _avatarView = [[UIImageView alloc]initWithImage:UIImageWithName(@"userinfo_avatar")];
    }
    return _avatarView;
}

-(UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColorFromHex(kColorMain) forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = UIFontSize(16);
        _loginBtn.layer.cornerRadius = 4.0;
//        _loginBtn.layer.maskedCorners = YES;
        _loginBtn.layer.borderColor = UIColorFromHex(kColorMain).CGColor;
        _loginBtn.layer.borderWidth = kOnePixel;
    }
    return _loginBtn;
}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.text = @"姓名";
        _nameLabel.font = UIFontSize(13);
        _nameLabel.textColor = UIColorFromHex(kColorBlack);
    }
    return _nameLabel;
}

-(UILabel *)schoolLabel
{
    if (!_schoolLabel) {
        _schoolLabel = [UILabel new];
        _schoolLabel.text = @"学校";
        _schoolLabel.font = UIFontSize(13);
        _schoolLabel.textColor = UIColorFromHex(kColorBlack);
        _schoolLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _schoolLabel;
}

-(UILabel *)classLabel
{
    if (!_classLabel) {
        _classLabel = [UILabel new];
        _classLabel.text = @"班级";
        _classLabel.font = UIFontSize(13);
        _classLabel.textColor = UIColorFromHex(kColorBlack);
        _classLabel.textAlignment = NSTextAlignmentRight;
    }
    return _classLabel;
}

@end
